package com.xgk.boot.framework.common.util.threadpool;

import com.xgk.boot.framework.common.context.ElmOrderThreadContext;
import org.springframework.core.task.TaskDecorator;

import java.util.HashMap;
import java.util.Map;

public class ContextCopyingDecorator implements TaskDecorator {
    @Override
    public Runnable decorate(Runnable runnable) {
        Map<String, Object> context = new HashMap<>(ElmOrderThreadContext.getAll());
        return () -> {
            try {
                ElmOrderThreadContext.setAll(context);
                runnable.run();
            } finally {
                ElmOrderThreadContext.clear();
            }
        };
    }
}
